package com.welab.springcloud.demo.user.controller;

import com.welab.springcloud.demo.dto.AuthRequest;
import com.welab.springcloud.demo.dto.ResponseResult;
import com.welab.springcloud.demo.dto.User;
import com.welab.springcloud.demo.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RefreshScope
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUser/{userId}")
    @ResponseBody
    public ResponseResult getUser(@PathVariable("userId") Integer userId) {
        User user = userService.getUserById(userId);
        return ResponseResult.success(user);
    }

    @PostMapping("/auth")
    @ResponseBody
    public ResponseResult auth(AuthRequest authRequest) {

        String authToken = userService.auth(authRequest);
        if (StringUtils.isBlank(authToken)) {
            return ResponseResult.fail();
        } else {
            return ResponseResult.success(authToken);
        }
    }

    @RequestMapping(value = "/parseToken")
    ResponseResult<Boolean> parseToken(@RequestParam String token) {
        log.info("parseToken:{}", token);
        return ResponseResult.success(userService.parseToken(token));
    }

}
